layui.use(['form', 'table', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form
    , table = layui.table
    , fsoft = layui.fsoft;
  var tableId = "sysRoleTable";
  var searchFormId = "fsoftSearchForm";
  //搜索监听
  form.on('submit(sreach)', function (data) {
    mAction.doSearch(data.field);
    return false;
  });
  //状态
  form.on('switch(status)', function (data) {
    var state = 1;
    var msg = "启用成功";
    if (data.elem.checked == false) {
      state = 0;
      msg = "禁用成功";
    }
    fsoft.post(layui.cache['contentPath'] + '/sys-role/enable', { id: this.value, status: state }, function (rs) {
      if (rs.code == 0) {
        fsoft.successMsg(msg);
      } else {
        fsoft.errorMsg(rs.msg);
      }
    });
    return false;
  });
  var mAction = {
    doSearch: function (params) {
      if (params == undefined || params == null) {
        params = $.extend({}, form.val(searchFormId));
      }
      table.render({
        elem: '#' + tableId,
        toolbar: '#batchBtnToolBar',
        url: layui.cache['contentPath'] + '/sys-role/findList'
        , where: params
        , cols: [[
          { type: 'checkbox', width: 50, fixed: 'left', align: 'center' },
          { type: 'numbers', width: 50, title: '序号', align: 'center' },
          { field: 'code', title: '角色编码', width: 150, align: 'center', style: "text-align:left", sort: true },
          { field: 'name', title: '角色名称', width: 200, align: 'center', style: "text-align:left" },
          { field: 'remark', title: '描述', minWidth: 200, width: 300, align: 'center', style: "text-align:left" },
          { field: 'status', title: '状态', width: 100, align: 'center', templet: '#switchStatus' },
          { fixed: 'right', width: 185, align: 'center', title: '操作', toolbar: '#sysrole-tabs' }
        ]],
        page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' }
        , height: 'full-108'
      });
    }
    , removeBatch: function (data) { // 删除
      if (data == undefined || data.length == 0) {
        layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
        return;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要删除当前记录？";
      if (ids.length > 1) {
        msg = "确认要删除已选中的（" + ids.length + "）条记录？";
      }
      fsoft.confirm("询 问", msg, function (index) {
        fsoft.delete(layui.cache['contentPath'] + '/sys-role/remove', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("删除成功");
            mAction.doSearch();
          } else {
            fsoft.errorMsg("删除失败！" + rs.msg);
          }
        });
        layer.close(index);
      });
    },
    showContentForm: function (data) {
      var path = layui.cache['contentPath'] + '/sys-role/add';
      if (data != undefined && data != null) {
        path = layui.cache['contentPath'] + '/sys-role/edit/' + data.id;
      }
      layer.open({
        type: 2
        , area: ["450px", '400px']
        , content: path
        , btn: ["确定", "关闭"]
        , yes: function (index) {
          var $body = layer.getChildFrame('body', index);
          var $button = $body.find('button[lay-submit]');
          $button.click();
        }
        , end: function () {
          mAction.doSearch();
        }
      });
    }
  };

  mAction.doSearch();
  /* 工具条 */
  table.on('toolbar(' + tableId + ')', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case 'add':
        mAction.showContentForm();
        break;
      case 'removeBatch':
        mAction.removeBatch(checkStatus.data);
        break;
    }
  });
  //行操作监听 
  table.on('tool(' + tableId + ')', function (obj) {
    var data = obj.data;
    if (obj.event === 'permission') { //查看详情
      layer.open({
        type: 2,
        title: '设置权限-[' + data.name + "]"
        , area: ['800px', '640px']
        , content: layui.cache['contentPath'] + '/sys-role/info/' + data.id
        , end: function () {

        }
      });
    } else if (obj.event === 'edit') { //编辑
      mAction.showContentForm(data);
    } else if (obj.event === 'del') { //删除
      mAction.removeBatch([data]);
    }
  });

});